package com.hoyo.service;

import java.util.ArrayList;
import java.util.List;

import javax.security.auth.login.AccountNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hoyo.common.AppConstant;
import com.hoyo.common.PageInfo;
import com.hoyo.domain.Comment;
import com.hoyo.domain.CommentReport;
import com.hoyo.domain.ReportItem;
import com.hoyo.domain.User;
import com.hoyo.domain.UserInfo;
import com.hoyo.domain.UserPermission;
import com.hoyo.mapper.CommentMapper;
import com.hoyo.mapper.UserMapper;
import com.hoyo.utils.MailUtils;
import com.hoyo.utils.MessageUtils;

@Service
public class CommentServiceImpl implements CommentService{
	
	@Autowired
	private CommentMapper commentMapper;
	
	@Autowired
	private UserMapper userMapper;

	@Override
	public PageInfo<Comment> pageFindReplyCommentsByCommentId(int commentId, int pageNum) throws Exception {
		List<Comment> comments = commentMapper.findReplyCommentsByCommentIdOrderByLikPointsDescAndLimitBy(commentId, (pageNum-1)*3, 3);
		int total = comments.size()>0?comments.get(0).getTotal():0;
		return new PageInfo<>(pageNum, 3, total, comments);
	}

	@Override
	@Transactional
	public PageInfo<Comment> newComment(int movieId, String account, String comment) throws Exception {
		
		User user = userMapper.findByIdentifier(account);
		if(user == null){
			throw new AccountNotFoundException();
		}
		Comment cmt = new Comment(movieId, user.getId(), comment);
		commentMapper.save(cmt);
		List<Comment> comments = commentMapper.findCommentsByMovieIdOrderByCreateTimeDescAndLimitBy(movieId, 0, 10);
		int total = comments.size()>0?comments.get(0).getTotal():0;
		return new PageInfo<>(1, 10, total, comments);
	}

	@Override
	public PageInfo<ReportItem> pageFindBeReportedComments(int pageNum, int pageSize) throws Exception {
		List<ReportItem> items = commentMapper.findBeReportCommentsByCheckOrderByCreateTimeDescLimitBy(false, (pageNum-1)*pageSize, pageSize);
		int total = items.size()>0?items.get(0).getTotal():0;
		return new PageInfo<ReportItem>(pageNum, pageSize, total, items);
	}

	@Override
	public PageInfo<ReportItem> reportHandle(int id, int userId, int actionType) throws Exception {
		System.out.println("id="+id);
		int commentId = commentMapper.findCommentIdByReportId(id);
		Comment comment = commentMapper.findOneById(commentId);
		switch (actionType) {
			case 0:
				commentMapper.updateIsSolvedAndIsReallyById(id, true, true);
				commentMapper.deleteOneById(comment.getCommentId());
				commentMapper.delteReplyCommentsByCommentId(comment.getCommentId());
				this.login4bidAndSendMessage(userId);
				break;
			case 1:
				commentMapper.updateIsSolvedAndIsReallyById(id, true, true);
				commentMapper.deleteOneById(comment.getCommentId());
				commentMapper.delteReplyCommentsByCommentId(comment.getCommentId());
				userMapper.saveUserPermission(new UserPermission(userId, "/comment/new"));
				break;
			case -1:
				commentMapper.updateIsSolvedAndIsReallyById(id, true, false);
				break;
	
			default:
				break;
		}
		return pageFindBeReportedComments(1, 3);
	}

	@Override
	public void newCommentReport(int movieId, int commentId, String account, String reason,String type) throws Exception {
		User user = userMapper.findByIdentifier(account);
		if(user == null){
			throw new AccountNotFoundException();
		}
		commentMapper.saveCommentReport(new CommentReport(movieId, user.getId(), commentId, reason,type));
	}

	@Override
	public PageInfo<Comment> pageFindComments(int pageNum, int pageSize) throws Exception {
		List<Comment> comments = commentMapper.findCommentsLimitBy((pageNum-1)*pageSize, pageSize);
		int total = comments.size()>0?comments.get(0).getTotal():0;
		return new PageInfo<Comment>(pageNum, pageSize, total, comments);
	}

	@Override
	public PageInfo<Comment> removeOne(int id) throws Exception {
		commentMapper.deleteOneById(id);
		commentMapper.delteReplyCommentsByCommentId(id);
		return this.pageFindComments(1, 5);
	}

	@Override
	public PageInfo<Comment> login4bid(int userId, int commentId) throws Exception {
		commentMapper.deleteOneById(commentId);
		commentMapper.delteReplyCommentsByCommentId(commentId);
		this.login4bidAndSendMessage(userId);
		return this.pageFindComments(1, 5);
	}
	
	private void login4bidAndSendMessage(int userId)throws Exception {
		userMapper.updateIsLockByUserId(true, userId);
		User user = userMapper.findById(userId);
		if(user == null){
			throw new AccountNotFoundException();
		}
		UserInfo userInfo = userMapper.findUserInfoByUserId(userId);
		String phone = user.getPhone();
		String nickname = userInfo.getNickname();
		if(phone == null || "".equals(phone)){
//				send email message
			MailUtils.send("hoyo", String.format(AppConstant.MESSAGE_CODE_TEMPLATE, nickname), user.getEmail());
		}else{
//				send telephone message
			String params = "{\"nickname\":\""+nickname+"\"}";
			MessageUtils.send(phone, AppConstant.Message.TEMPLATE_MESSAGE, params);
		}
	}

	@Override
	public PageInfo<Comment> comment4bid(int userId, int commentId) throws Exception {
		userMapper.saveUserPermission(new UserPermission(userId, "/comment/new"));
		commentMapper.deleteOneById(commentId);
		commentMapper.delteReplyCommentsByCommentId(commentId);
		return this.pageFindComments(1, 5);
	}

	@Override
	public PageInfo<Comment> newReplyComment(Comment comment,User user) throws Exception {
	/*	User user = userMapper.findByIdentifier(telephone);
		if(user == null){
			throw new AccountNotFoundException();
		}*/
		comment.setUserId(user.getId());
		UserInfo userInfo = userMapper.findUserInfoByUserId(comment.getReplyUserId());
		comment.setReplyUserNickname(userInfo.getNickname());
		System.out.println(comment);
		commentMapper.saveReplyComment(comment);
		int total = commentMapper.selectCountFromReCommentByCommentId(comment.getCommentId());
		PageInfo<Object> pageInfo = new PageInfo<>(1, 3, total, new ArrayList<>());
		return this.pageFindReplyCommentsByCommentId(comment.getCommentId(), pageInfo.getPages());
	}
}
